package svc

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/config"
	websocket "gitee.com/liuxuezhan/ar-platform/base-common/net/websoket"
	"gitee.com/liuxuezhan/ar-platform/base-common/rpc"
	"gitee.com/liuxuezhan/ar-platform/base-common/rpc/interceptors"
	clientConfig "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/config"
	clientPayRecharge "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/payrecharge"
	clientPayWithdraw "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/paywithdraw"
	clientRpcslotgame "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/rpcslotgame"
	clientTenantChannel "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/tenantchannel"
	clientUser "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/user"
	clientUserChannel "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/userchannel"
	clientUserWash "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/userwash"
	loginUser "gitee.com/liuxuezhan/ar-platform/rpc-client/loginClient/client"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/settingtoc"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userservice"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config           config.Config
	AssetsRpc        AssetsRpc
	LoginRPC         loginUser.User
	CommonSettingRpc settingtoc.SettingToC
	UserServiceRPC   userservice.UserService
	UserRpc          userservice.UserService
	WSHub            *websocket.Hub
}

type AssetsRpc struct {
	Config        clientConfig.Config
	PayRecharge   clientPayRecharge.PayRecharge
	PayWithdraw   clientPayWithdraw.PayWithdraw
	RpcSlotGame   clientRpcslotgame.RpcSlotGame
	TenantChannel clientTenantChannel.TenantChannel
	User          clientUser.User
	UserWash      clientUserWash.UserWash
	UserChannel   clientUserChannel.UserChannel
}

func assetsRpc(c config.Config) AssetsRpc {
	// 自定义拦截器
	client := zrpc.MustNewClient(
		zrpc.RpcClientConf{
			Target: rpc.GenRpcTarget(c.AssetsRPC),
		},
		zrpc.WithUnaryClientInterceptor(
			interceptors.ClientErrorInterceptor(c.Name, c.Name),
		),
	)
	return AssetsRpc{
		Config:        clientConfig.NewConfig(client),
		PayRecharge:   clientPayRecharge.NewPayRecharge(client),
		PayWithdraw:   clientPayWithdraw.NewPayWithdraw(client),
		RpcSlotGame:   clientRpcslotgame.NewRpcSlotGame(client),
		TenantChannel: clientTenantChannel.NewTenantChannel(client),
		User:          clientUser.NewUser(client),
		UserWash:      clientUserWash.NewUserWash(client),
		UserChannel:   clientUserChannel.NewUserChannel(client),
	}
}

func buildCommonSettingRpc(c config.Config) settingtoc.SettingToC {
	settingRpc, err := rpc.NewClient(rpc.Config{
		Host:     c.SettingRpc,
		AppName:  c.Name,
		Business: c.Name,
	})
	if err != nil {
		logc.Error(context.Background(), err)
	}

	return settingtoc.NewSettingToC(settingRpc)
}

func NewServiceContext(c config.Config) *ServiceContext {
	loginRPC := zrpc.MustNewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.LoginRpc),
	}, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	loginRPCClient := loginUser.NewUser(loginRPC)

	var UserRpc zrpc.Client
	// UserRpc := zrpc.MustNewClient(zrpc.RpcClientConf{
	// 	Target: rpc.GenRpcTarget(c.UserRpc),
	// }, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	userRpc := userservice.NewUserService(UserRpc)

	wsHub := websocket.NewHub()
	go wsHub.Run()

	// return &ServiceContext{
	// 	Config: c,
	// 	WSHub:  wsHub,
	// 	// 初始化其他服务...
	// }

	return &ServiceContext{
		Config:           c,
		AssetsRpc:        assetsRpc(c),
		LoginRPC:         loginRPCClient,
		CommonSettingRpc: buildCommonSettingRpc(c),
		UserRpc:          buildUserRpc(c),
		UserServiceRPC:   userRpc,
	}
}
func buildUserRpc(c config.Config) userservice.UserService {
	userRpc, err := rpc.NewClient(rpc.Config{
		Host:     c.UserRpc,
		AppName:  c.Name,
		Business: c.Name,
	})
	if err != nil {
		logc.Error(context.Background(), err)
	}

	return userservice.NewUserService(userRpc)
}
